﻿/* Copyright (c) 2011-2012, Zetatron Consulting
All rights reserved.

Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:

* Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.

* Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution.

* Neither the name of Zetatron Consulting nor the names of its contributors may be used to endorse or promote products derived from this software without specific prior written permission.

THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using WorldServer.Models;
using AionCxxLibrary.Utilities;
using AionCxxLibrary.Enums;
using System.Threading;
using WorldServer.Packets.Server;
using WorldServer.Data.Templates;
using WorldServer.Data;

namespace WorldServer.Services
{
    public class SkillService
    {
        static List<SkillLearnTemplate> FindNewSkills(Character c)
        {
            List<SkillLearnTemplate> Found = Datastore.SkillLearns.Values.ToList().FindAll(slt => slt.CharLevel <= c.Template.Level //&& slt.AutoLearn
                && (slt.Class == c.Template.Class || slt.Class == CharacterClass.ALL)
                && (slt.Race == "all" || (slt.Race == "pc_light" && c.Template.Race == Race.Elyos) || (slt.Race == "pc_dark" && c.Template.Race == Race.Asmodians))
                && c.SkillList.Find(se => se.SkillID == Datastore.SkillsByName[slt.SkillName].ID) == null);
            foreach (SkillLearnTemplate slt in Found)
            {
                Log.Debug("Found Skill for player: " + slt.SkillName);
            }
            return Found;
        }

        public static void CheckSkillList(Character c)
        {
            List<SkillLearnTemplate> newskills = FindNewSkills(c);
            if (newskills.Count > 0)
            {
                foreach (SkillLearnTemplate slt in newskills)
                {
                    SkillTemplate sk = Datastore.SkillsByName[slt.SkillName];
                    SkillEntry existing = c.SkillList.Find(se => se.SkillID == sk.ID);
                    if (existing != null && existing.Level >= slt.SkillLevel)
                        return;
                    if (existing != null)
                        c.SkillList.Remove(existing);                    
                    c.SkillList.Add(new SkillEntry() { SkillID = sk.ID, Level = slt.SkillLevel });
                }
            }
        }
        
        public static void UseSkill(Character c, ushort skillId, byte level, byte targetType, int targetId, float x, float y, float z, ushort ctime)
        {
            lock (c.Mutex_UseSkill)
            {
                SkillTemplate skillTemplate = Datastore.SkillsById[skillId];
                Log.Debug(c.Template.Name + " SkillUse " + skillTemplate.Name);

                if (c.CurrentAction != null)
                {
                    Log.Hack(c.Template.Name + " trying to use " + skillTemplate.Name + " but another action is in progress !");
                    return;
                }

                if (c.SkillList.Find(se => se.SkillID == skillId && se.Level >= level) == null)
                {
                    Log.Hack(c.Template.Name + " trying to use " + skillTemplate.Name + " lv. " + level + " not present in SkillList!");
                    return;
                }

                AionObject target = null;
                if(targetId > 0)
                    target = LevelService.Objects[targetId];

                CastedSkill skill = new CastedSkill(c, target, skillTemplate, level, targetType, x, y, z, ctime);

                c.CurrentAction = new AionAction() { Type = AionActionType.UseSkill, ArgInt = skillTemplate.ID };

                skill.Pkt_Init();

                c.Move += new AionObject.ObjectMoveHandler((sender, args) =>
                {
                    skill.CancelCast();
                });

                // Schedule postuse tasks
                Timer timer = ThreadPoolManager.Schedule(skillTemplate.CastingDelay + 1, new Action(delegate()
                {
                    c.CurrentAction = null;
                    skill.CastComplete = true;
                    skill.Pkt_Use();
                }));
                skill.CastTimer = timer;

                c.CurrentAction = new AionAction() { Type = AionActionType.UseSkill, ArgInt = skillId, ArgObject = timer };

            }
        }

    }
}
